package jwt

import (
	"fmt"

	"github.com/gin-gonic/gin"
	"xdingtec.cn/xdkaika/app"
	"xdingtec.cn/xdkaika/utils/jwt"
	"xdingtec.cn/xdkaika/utils/response"
)

// jwt token 验证
func Next(urlExclude []string) gin.HandlerFunc {
	return func(c *gin.Context) {
		//auth
		url := c.Request.URL.Path
		checkExclude := false
		for _, u := range urlExclude {
			if u == url {
				checkExclude = true
			}
		}

		claims, err := jwt.ValidateToken(c.Request.Header.Get("Authorization"))

		fmt.Println("jwt````````````````````````````````````````", url,
			checkExclude, urlExclude, claims, err, c.Request.Header.Get("Authorization"))
		if !checkExclude && (err != nil || claims.UserId < 1) {
			response.ErrorTokenAuthFail(c)
			return
		}
		// 赋予全局调用
		if claims != nil {
			app.JWTClaims = claims
		}
		fmt.Println("jwt````````````````````````````````````````", url, checkExclude, urlExclude, claims, err)
		//
		c.Next()
	}
}
